<?php

/**
 * PESCMS for PHP 5.4+
 *
 * Copyright (c) 2014 PESCMS (http://www.pescms.com)
 *
 * For the full copyright and license information, please view
 * the file LICENSE.md that was distributed with this source code.
 */

namespace Model;

/**
 * 时间日志模型
 */
class Timelog extends \Core\Model\Model {

    public static function tmList($user = '') {
        $page = new \Expand\Page;
        $page->LANG = [
            'pre' => '&laquo;',
            'next' => '&raquo;',
        ];
        $page->style = [
            'total' => '',
            'first' => '',
            'pre' => '<li>{pre}</li>',
            'pagenumber' => '<li>{pagenumber}</li>',
            'current' => '<li class="am-active">{current}</li>',
            'next' => '<li>{next}</li>',
            'last' => ''
        ];

        $conititon = "timelog_delete = 0 ";
        $param = array();

        if (!empty($_GET['type'])) {
            $conititon .= " AND tl.timelog_type = :timelog_type";
            $param['timelog_type'] = self::g('type');
        }

        if (!empty($user)) {
            $conititon .= " AND tl.user_id = :user_id";
            $param['user_id'] = $user;
        }

        $total = self::db('timelog AS tl')->field('count(tl.timelog_id) AS total')->join(self::$modelPrefix . 'user AS u ON u.user_id = tl.user_id')->join(self::$modelPrefix . 'type AS t ON t.type_id = tl.timelog_type')->where($conititon)->find($param)['total'];
        $page->total($total);
        $page->handle();

        $list = self::db('timelog AS tl')->field('tl.*, u.user_id, u.user_name, u.user_head, t.*')->join(self::$modelPrefix . 'user AS u ON u.user_id = tl.user_id')->join(self::$modelPrefix . 'type AS t ON t.type_id = tl.timelog_type')->where($conititon)->order('tl.timelog_updatetime DESC')->limit("{$page->firstRow}, {$page->listRows}")->select($param);

        $show = $page->show();

        return array('list' => $list, 'page' => $show);
    }

}
